/*

The MIT License

Copyright (c) 2009 kuneri-api contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

// Read docs for more info:
// http://wiki.kunerilite.net/index.php?title=Camera_plugin

import kuneri.Plugin;
import kuneri.Response;
import kuneri.Configuration;
import kuneri.CameraResponse;

class kuneri.Camera extends Plugin
{
	
	public function Camera(configuration : Configuration)
	{
		name = "camera";
		super(configuration);
	}
	
	public function prepare(callback : Function, index : Number) : Void
	{
		
		if(index == null)
		{
			index = MAIN_CAMERA;
		}
		
		userCallback = callback;
		request(COMMAND_PREPARE, {klIndex:index});
	}
	
	public function start(callback : Function, index : Number, path : String, mode : String, size : String, instant : Number, time : Number, flash : Number) : Void
	{
		
		var params : Object = new Object();
		
		params.klIndex = (index == null)? MAIN_CAMERA : index;
		params.klPath = (path == null)? DEFAULT_IMAGE_PATH : path;
		params.klMode = (mode == null)? MODE_PICTURE : mode;
		params.klSize = (size == null)? SIZE_MEDIUM : size;
		params.klInstant = (instant == null)? 1 : instant;
		params.klTime = (time == null)? 0 : time;
		params.klFlash = (flash == null)? FLASH : flash;
		
		userCallback = callback;
		request(COMMAND_START, params);
		
	}
	
	public function status(callback : Function) : Void
	{
		userCallback = callback;
		request(COMMAND_STATUS);
	}
	
	public function resize(callback : Function, path : String, targetFile : String, size : String, overwrite : Boolean) : Void
	{
		
		userCallback = callback;
		
		var params : Object = new Object();
		params.klPath = path;
		params.klTargetFile = targetFile;
		params.klSize = size;
		params.klOverwrite = (overwrite == null)? true : overwrite;
		
		request(COMMAND_RESIZE, params);
		
	}
	
	private function requestHandler(data : String, command : String) : Void
	{
		
		super.requestHandler(data, command);
		
		var response : CameraResponse = new CameraResponse();
		response.command = command;
		response.error = Number(responseObject[CameraResponse.KL_ERROR]);
		response.end = Number(responseObject[CameraResponse.KL_END]);
		response.responseObject = responseObject;
		
		if(response.error == CameraResponse.SUCCESS)
		{
			
			switch(command)
			{
				case COMMAND_STATUS:
					
					response.status = responseObject["klStatus"];
					break;
					
				case COMMAND_RESIZE:
					
					if(responseObject["klTargetFile"] != null)
					{
						response.generatedFiled = responseObject["klTargetFile"];
					}
					
					break;
					
			}
			
		}
		
		userCallback(response);
		
	}
	
	private var userCallback : Function;
	
	public static var COMMAND_PREPARE : String = "prepare";
	
	public static var COMMAND_START : String = "start";
	
	public static var COMMAND_STATUS : String = "status";
	
	public static var COMMAND_RESIZE : String = "resize";
	
	public static var SIZE_SMALL: String = "small";
	
	public static var SIZE_MEDIUM: String = "medium";
	
	public static var SIZE_FULL: String = "full";
	
	public static var MODE_VIDEO : String = "video";
	
	public static var MODE_PICTURE : String = "picture";
	
	public static var FLASH : Number = 1;
	
	public static var NO_FLASH : Number = 0; // Not 100% sure, Kuneri Docs don't specify
	
	public static var INSTANT : Number = 1;
	
	public static var MAIN_CAMERA : Number = 0;
	
	public static var FRONT_CAMERA : Number = 1;
	
	public static var DEFAULT_IMAGE_PATH : String = "\\Data\\Images\\image.jpg";
	
}